<template>
  <basic-container>
    <avue-crud ref="crud"
               :option="tableOption" :data="tableData" :table-loading="tableLoading" :page="page"
               :permission="permission"
               @current-change="handleTablePageChange"
               @refresh-change="handleTableData"
               @row-save="handleSave" @row-update="handleUpdate" @row-del="handleDel">
    </avue-crud>
  </basic-container>
</template>

<script>
  import { getTableOption, getTableData, createAt, deleteAt, updateAt } from './api'

  let thatTableOption = getTableOption()

  export default {
    name: "widget-notice",
    components: {},
    data () {
      return {
        tableOption: thatTableOption, //表格设置属性
        tableData: [], //表格的数据
        tablePage: 1,
        tableLoading: false,
        permission: {},
        options: [],
        page: {
          total: 0, //总页数
          currentPage: 1, //当前页数
          pageSize: 10 //每页显示多少条
        }
      }
    },
    created () {
      this.handleTableData()
    },
    watch: {
      options() {
        let btns = {
          viewBtn:false,
          addBtn:false,
          delBtn:false,
          editBtn:false
        }
        this.options.forEach((opt) => {
          switch (opt) {
            case 'view':
              btns.viewBtn = true
              break
            case 'create':
              btns.addBtn = true
              break
            case 'delete':
              btns.delBtn = true
              break
            case 'update':
              btns.editBtn = true
              break
          }
        })
        this.permission = btns
      }
    },
    mounted () { },
    computed: {
    },
    props: [],
    methods: {
      /**
       * @title 加载表数据
       */
      handleTableData() {
        this.tableLoading = true
        if(!this.page.currentPage)
          this.page.currentPage = 1
        getTableData({ page_index: this.page.currentPage })
            .then(res => {
              this.page.total = res.data.total_count
              this.page.pageSize = res.data.page_size
              this.tableData = res.data.data
              this.options = res.data.permissions
              this.tableLoading = false
            })
      },
      /**
       * @title 列表页改变
       */
      handleTablePageChange(index) {
        this.page.currentPage = index
        this.handleTableData()
      },
      /**
       * @title 数据添加
       * @param row 为当前的数据
       * @param done 为表单关闭函数
       *
       **/
      handleSave (row, done) {
        createAt(row)
            .then(res => {
              let data = res.data.data
              this.tableData.push(data)
              this.$message({
                showClose: true,
                message: "添加成功",
                type: "success"
              })
              done()
            })
      },
      /**
       * @title 数据更新
       * @param row 为当前的数据
       * @param index 为当前更新数据的行数
       * @param done 为表单关闭函数
       *
       **/
      handleUpdate (row, index, done) {
        updateAt(row)
            .then(res => {
              let data = res.data.data
              this.tableData.splice(index, 1, data)
              this.$message({
                showClose: true,
                message: "修改成功",
                type: "success"
              })
              done()
            })
      },
      /**
       * @title 数据删除
       * @param row 为当前的数据
       * @param index 为当前更新数据的行数
       *
       **/
      handleDel (row, index) {
        this.$confirm(`是否确认删除名为'${row.title}'的公告`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteAt(row)
              .then(() => {
                this.tableData.splice(index, 1)
                this.$message({
                  showClose: true,
                  message: "删除成功",
                  type: "success"
                })
              })
        }).catch(() => { })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .table-container {
    padding: 8px 10px;
  }
  .avue-crud {
    height:100%;
  }
</style>

